package chu.dejian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sam.blemodule.R;
import com.sam.utils.L;
import com.sam.utils.T;

import java.util.List;

import chu.dejian.entity.ContactEntity;
import chu.dejian.models.PostAttenderRequestRequest;
import chu.dejian.models.PostAttenderRequestResponse;
import chu.dejian.restApi.RestClient;
import chu.dejian.widget.AttentionLevelView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class AttentionAdapter extends BaseAdapter {
    protected Context context;
    private List<ContactEntity> contactEntityList;
    private int layoutResId;
    private boolean levelEditable = false;
    private static AttentionLevelView.ATTENTION_LEVEL orignLevel;

    public AttentionAdapter(Context context,int layoutResId, List<ContactEntity> contactEntityList,boolean levelEditable) {
        this.context = context;
        this.contactEntityList = contactEntityList;
        this.layoutResId = layoutResId;
        this.levelEditable = levelEditable;
    }

    @Override
    public int getCount() {
        return contactEntityList.size();
    }

    @Override
    public ContactEntity getItem(int position) {
//        return contactEntityList.get(getCount() - 1 - position);
        return contactEntityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    AttentionLevelView.ATTENTION_LEVEL[] levels = new AttentionLevelView.ATTENTION_LEVEL[]{
            AttentionLevelView.ATTENTION_LEVEL.NOTIFY_ALL,
            AttentionLevelView.ATTENTION_LEVEL.NOTIFY_WARNING,
            AttentionLevelView.ATTENTION_LEVEL.NOTIFY_EMERGENT,
            AttentionLevelView.ATTENTION_LEVEL.NOTIFY_NOTHING
    };

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(layoutResId, null, false);
            viewHolder.attentionName = (TextView) convertView.findViewById(R.id.contact_name);
            viewHolder.attentionPhoneNum = (TextView) convertView.findViewById(R.id.contact_num);
            viewHolder.levelView = (AttentionLevelView) convertView.findViewById(R.id.attention_level_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if(contactEntityList != null && contactEntityList.size() != 0){  // 调试模式下可能为null
            final ContactEntity contactEntity = getItem(position);
            viewHolder.attentionName.setText(contactEntity.contactName);
            viewHolder.attentionPhoneNum.setText(contactEntity.contactPhoneNum);
            viewHolder.levelView.setLevel(contactEntity.attentionLevel);
            viewHolder.levelView.setEditable(levelEditable);
            if(!levelEditable) return convertView;
            viewHolder.levelView.setSpinnerCallBack(new AttentionLevelView.SpinnerCallBack() {
                @Override
                public void onSpinnerOpen() {
                    orignLevel = viewHolder.levelView.getAttentionLevel();
                }

                @Override
                public void onSpinnerClose() {
                    if(orignLevel == viewHolder.levelView.getAttentionLevel()) return;
                    PostAttenderRequestRequest request = new PostAttenderRequestRequest(contactEntity.contactPhoneNum, viewHolder.levelView.getAttentionLevelValue(), contactEntity.contactName);
                    RestClient.getAuthedClient().getApiService().postAttenderRequest(request, new Callback<PostAttenderRequestResponse>() {
                        @Override
                        public void success(PostAttenderRequestResponse postAttenderRequestResponse, Response response) {
                            String requestStr = postAttenderRequestResponse == null ? "null" : postAttenderRequestResponse.toString();
                            L.d("《用户提交关注请求-postAttenderRequest》" + "\n" + "请求成功： \n\t返回数据：" + requestStr);// + "\n\t响应数据：" + printResponse(response));
                            if(response.getStatus() != 201){
                                T.showLong(context,R.string.level_change_failed);
                                viewHolder.levelView.setLevel(orignLevel);
                            }
                        }

                        @Override
                        public void failure(RetrofitError retrofitError) {
                            L.d("《用户提交关注请求-postAttenderRequest》"+"\n"+ "请求失败： \n\t错误信息：" + retrofitError.toString());
                            T.showLong(context,R.string.level_change_failed);
                            viewHolder.levelView.setLevel(orignLevel);
                        }
                    });
                }
            });
        }
        return convertView;
    }


    private class ViewHolder {
        TextView attentionName,attentionPhoneNum;
        AttentionLevelView levelView;
    }

}
